<template>
    <Modal v-model:visible="isShowModal" :title="`${editId ? '编辑' : '添加'}账号`" @ok="handleOk" @cancel="handleCancel"
        ok-text="提交" cancel-text="取消" :loading="loading">
        <Form :model="formState" ref="formRef" :label-col="{ span: 4 }" :wrapper-col="{ span: 18 }" :rules="rules">
            <FormItem label="姓名" name="name" :required="true">
                <Input v-model:value="formState.name" placeholder="请输入姓名" allowClear></Input>
            </FormItem>
            <FormItem label="手机号" name="mobile" :required="true">
                <Input v-model:value="formState.mobile" placeholder="请输入手机号" allowClear></Input>
            </FormItem>
            <FormItem label="密码" name="pwd" :required="editId ? false : true">
                <InputPassword v-model:value="formState.pwd" placeholder="请输入密码" allowClear></InputPassword>
            </FormItem>
            <FormItem label="邮箱" name="email">
                <Input v-model:value="formState.email" placeholder="请输入邮箱" allowClear></Input>
            </FormItem>
            <FormItem label="是否超管" name="isAdmin">
                <RadioGroup v-model:value="formState.isAdmin" :options="EnumIsAdminUser" />
            </FormItem>
            <FormItem label="状态" name="state">
                <Select v-model:value="formState.state" style="width: 100%" placeholder="请选择状态" allowClear>
                    <SelectOption :value="item.value" :label="item.label" :key="item.value"
                        v-for="item in AdminUserState">
                        <span>{{ item.label }}</span>
                    </SelectOption>
                </Select>
            </FormItem>
        </Form>
    </Modal>
</template>

<script lang="ts" setup>
import { Modal, Form, Input, message, Select, RadioGroup } from 'ant-design-vue'
import { Rule } from 'ant-design-vue/es/form';
import { EnumIsAdminUser, AdminUserState } from './index'
import { AdminUserAddModel, AdminUserApi, AdminUserEditModel } from '@/api';

const FormItem = Form.Item;
const InputPassword = Input.Password;
const SelectOption = Select.Option;

const emit = defineEmits(['ok']);

const isShowModal = ref(false)
const loading = ref(false)
const editId = ref<number>();
const formState = ref<AdminUserAddModel>({
    name: '',
    pwd: '',
    mobile: '',
    isAdmin: true,
})
const formRef = ref()
const rules: Record<string, Rule[]> = {
    name: [{ required: true, message: "请输入姓名", trigger: 'blur' }],
    mobile: [{ required: true, message: "请输入手机号", trigger: 'blur' }],
    pwd: [{ message: "请输入密码", trigger: 'blur' }],
};
const handleOpen = (data: AdminUserEditModel) => {
    if (data) {
        editId.value = data.id;
        formState.value = {
            name: data.name,
            email: data.email,
            mobile: data.mobile as any,
            isAdmin: data.isAdmin,
            state: data.state,
            pwd: data.pwd || '',
        }
    } else {
        handleEmpty();
    }
    isShowModal.value = true;
}

const handleOk = () => {
    loading.value = true;
    formRef.value
        .validate()
        .then(async () => {
            if (!formState.value.email) {
                formState.value.email = undefined;
            }
            if (editId.value) {
                let params = {
                    id: editId.value,
                    ...formState.value
                }
                await AdminUserApi.adminuserAdminedit(params)
                message.success('修改成功')
            } else {
                await AdminUserApi.adminuserAdminadd(formState.value)
                message.success('添加成功')
            }

            emit('ok')
            handleClose();
        })
        .catch((err: any) => {
            console.log('校验失败', err);
        }).finally(() => {
            loading.value = false;
        });
}

const handleEmpty = () => {
    editId.value = undefined;
    formState.value = {
        name: '',
        mobile: '',
        pwd: '',
        isAdmin: true,
    };
}

function handleClose() {
    isShowModal.value = false;
}

function handleCancel() {
    handleEmpty();
    handleClose();
}

defineExpose({ handleOpen })
</script>